package com.leyou.web;

import com.leyou.common.vo.PageResultVO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author xiuer
 * @description 品牌控制层 http://api.leyou.com/api/item/brand
 * @create 2019/6/24 15:44
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private IBrandService brandService;

    /**
     * 分页查询品牌信息
     *
     * @param key    搜索条件
     * @param page   当前显示的页数
     * @param rows   每页显示的条数
     * @param sortBy 排序的规则
     * @param desc   是否降序
     */
    @GetMapping("page")
    public ResponseEntity<PageResultVO<BrandDTO>> brandQueryPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", defaultValue = "id") String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc) {
        return ResponseEntity.ok(brandService.brandQueryPage(key, page, rows, sortBy, desc));
    }

    /**
     * 新增品牌
     *
     * @param cids 商品分类和品牌中间表 cid商品主键
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.addBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改品牌和该品牌对应的分类
     */     //http://api.leyou.com/api/item/brand Request Method: PUT
    // id: 325406 name: 第四次试试    // image: // cids: 3,4,5,6  // letter: A
    @PutMapping
    public ResponseEntity<Void> updateBrandAndCategory(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.updateBrandAndCategory(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除品牌及删除该品牌的商品分类
     * http://api.leyou.com/api/item/brand/?id=6522
     */
    @DeleteMapping
    public ResponseEntity<Void> deleteBrandByBid(@RequestParam("id") Long bid) {
        brandService.deleteBrandByBid(bid);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据三级分类id查询所有品牌信息
     * http://api.leyou.com/api/item/brand/of/category?id=3
     *
     * @Param 三级分类id
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandBycid(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(brandService.queryBrandBycid(cid));
    }

    /**
     * 根据品牌id查询品牌
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandByBid(@PathVariable("id") Long bid) {
        return ResponseEntity.ok(brandService.queryBrandByBid(bid));
    }
    /**
     * 根据品牌id集合查询品牌
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByBids(@RequestParam("bids") List<Long> bids) {
        return ResponseEntity.ok(brandService.queryBrandByBids(bids));
    }
}
